﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Diagnostics;
using DevComponents.DotNetBar.Rendering;
namespace LanAnh
{
    public partial class frmMain_2 :Office2007RibbonForm
    {
        public frmQLCongDan QLCongDan;
        public frmKhachHang qlKhachHang;
        public frmNhaCC qlNhaCungCap;
        public frmSanPham qlSanPham;
        public frmDatHang qlDatHang;

        Data data;
        string sql;
        public frmMain_2()
        {
            InitializeComponent();            
        }
        #region Theme
        //===========Theme================
        private void buttonBlackScheme_Click(object sender, EventArgs e)
        {
            RibbonPredefinedColorSchemes.ChangeOffice2007ColorTable(this, eOffice2007ColorScheme.Black);
        }
        private void buttonSilverScheme_Click(object sender, EventArgs e)
        {
            RibbonPredefinedColorSchemes.ChangeOffice2007ColorTable(this, eOffice2007ColorScheme.Silver);
        }
        private void buttonBlueScheme_Click(object sender, EventArgs e)
        {
            RibbonPredefinedColorSchemes.ChangeOffice2007ColorTable(this, eOffice2007ColorScheme.Blue);
        }
        private bool m_ColorSelected = false;
        private eOffice2007ColorScheme m_BaseColorScheme = eOffice2007ColorScheme.Blue;
        private void colorPickerCustomScheme_ColorPreview(object sender, DevComponents.DotNetBar.ColorPreviewEventArgs e)
        {
            RibbonPredefinedColorSchemes.ChangeOffice2007ColorTable(this, m_BaseColorScheme, e.Color);
        }
        private void colorPickerCustomScheme_ExpandChange(object sender, System.EventArgs e)
        {

        }
        private void colorPickerCustomScheme_SelectedColorChanged(object sender, EventArgs e)
        {
            m_ColorSelected = true;
            RibbonPredefinedColorSchemes.ChangeOffice2007ColorTable(this, m_BaseColorScheme, colorPickerCustomScheme.SelectedColor);
        }
        //===============End Theme==============
        #endregion
        private void frmMain_2_Load(object sender, EventArgs e)
        {
            try
            {
                data =  new Data();
                if (!data.CheckConnection())
                {
                    throw new Exception();
                }
            }
            catch
            {
                (new frmConnectDatabase()).ShowDialog();
            }
            //if (batdau == null || batdau.IsDisposed)
            //{
            //    batdau = new frmStartup();
            //    batdau.MdiParent = frmMain.ActiveForm;
            //    batdau.Show();

            //}
            //else
            //    batdau.Activate();
            //ribbonControl1.Expanded = true;
            Status.lblStatus = lbNguoiTruyCap;
            btnDangNhap_Click(sender, e);
        }
        private void btnClose_Menu_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    

       

        private void btnNhanKhau_Click(object sender, EventArgs e)
        {
            if (QLCongDan == null || QLCongDan.IsDisposed)
            {
                QLCongDan = new frmQLCongDan(QuyenHan);
                QLCongDan.MdiParent = frmMain_2.ActiveForm;
                QLCongDan.Show();

            }
            else
                QLCongDan.Activate();
        }

      

        private void btnDangNhap_Click(object sender, EventArgs e)
        {
            if (btnDangNhap.Text == "Đăng Nhập")
            {
                frmDangNhap frm = new frmDangNhap();
                frm.ShowDialog();
                if (DaDangNhap == true)
                {
                    btnDoiPass.Visible = true;
                    ribbonQL.Enabled = true;
                    ribbonThongKe.Enabled = true;
                    btnQuanLi.Enabled = true;
                    
                    btnThongke.Enabled = true;
                    btnDangNhap.Text = "Đăng Xuất";                    

                    Status.TrangThai("Người truy cập: " + NguoiDangNhap);
                    if (QuyenHan == "Toàn Quyền")
                    {
                        btnAdmin.Enabled = true;
                        ribbonAdmin.Enabled = true;
                    }
                    else if (QuyenHan == "")
                    {
                        btnAdmin.Enabled = false;
                        ribbonAdmin.Enabled = false;
                    }
                }
                else
                {
                    ribbonAdmin.Enabled = false;
                    ribbonQL.Enabled = false;
                    ribbonThongKe.Enabled = false;
                    btnQuanLi.Enabled = false;
                    btnAdmin.Enabled = false;
                    btnThongke.Enabled = false;
                    btnDangNhap.Text = "Đăng Nhập";
                }
            }
            else if (btnDangNhap.Text == "Đăng Xuất")
            {
                btnDoiPass.Visible = false;
                ribbonAdmin.Enabled = false;
                ribbonQL.Enabled = false;
                ribbonThongKe.Enabled = false;
                btnQuanLi.Enabled = false;
                btnAdmin.Enabled = false;
                btnThongke.Enabled = false;
                lbNguoiTruyCap.Text = "Chưa Đăng Nhập";
               
                try
                {
                    QLCongDan.Dispose();
                }
                catch { }
               
                DaDangNhap = false;
                QuyenHan = "";
                btnDangNhap.Text = "Đăng Nhập";
                btnDangNhap_Click(sender, e);

            }

        }
        static string TenDangNhap = "";
        static string QuyenHan = "";
        static string NguoiDangNhap = "";
        static bool DaDangNhap = false;
        public bool sktDangNhap(bool DangNhapOk)
        {
            DaDangNhap = DangNhapOk;
            return DaDangNhap;
        }
        public string sHoTen(string HoTen)
        {
            NguoiDangNhap = HoTen;
            return NguoiDangNhap;
        }
        public string sSessionTen(string Ten)
        {
            TenDangNhap = Ten;
            return TenDangNhap;
        }
        public string sSessionQuyen(string Quyen)
        {
            QuyenHan = Quyen;
            return QuyenHan;
        }
        private void btnQLNguoiDung_Click(object sender, EventArgs e)
        {
            frmNguoiDungMoi newNguoiDung = new frmNguoiDungMoi();
            newNguoiDung.ShowDialog();
        }

        private void btnDoiPass_Click(object sender, EventArgs e)
        {
            frmDoiMatKhau doipass = new frmDoiMatKhau(TenDangNhap);
            doipass.ShowDialog();
        }

        private void btnSaoLuu_Click(object sender, EventArgs e)
        {
            if (backupDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    sql = "BACKUP DATABASE NhanKhau TO DISK = '" + backupDialog.FileName.ToString() + "'";
                    data.executeNonQuery(sql);
                    MessageBoxEx.Show("Sao lưu dữ liệu thành công!", "BACKUP COMPLETED", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch { }
            }
            else
                return;
        }

        private void btnPhucHoi_Click(object sender, EventArgs e)
        {

            if (restoreDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    sql = "USE master RESTORE DATABASE NhanKhau FROM DISK = '" + restoreDialog.FileName.ToString() + "'";
                    data.executeNonQuery(sql);
                    MessageBoxEx.Show("Phục hồi dữ liệu thành công!", "RESTORE COMPLETED", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch { }
            }
            else
                return;


        }

       

        private void buttonItem3_Click(object sender, EventArgs e)
        {
            
        }

        private void buttonItem1_Click(object sender, EventArgs e)
        {
            btnDangNhap_Click(null, null);
        }

        private void tabStrip1_TabItemOpen(object sender, EventArgs e)
        {
            ribbonControl1.Expanded = false;
        }

        private void tabStrip1_TabsCleared(object sender, EventArgs e)
        {
            
        }

        private void tabStrip1_TabRemoved(object sender, EventArgs e)
        {
            ribbonControl1.Expanded = true;
        }

        private void ribbonControl1_Click(object sender, EventArgs e)
        {

        }

        private void btnCauHinh_Click(object sender, EventArgs e)
        {
            frmConnectDatabase connDB = new frmConnectDatabase();
            connDB.ShowDialog();
        }

        private void btnTeamviewer_Click(object sender, EventArgs e)
        {
            //System.Diagnostic.Process.Start("TeamViewerQS.exe");
            try
            {
                ProcessStartInfo startInfo = new ProcessStartInfo("TeamViewerQS.exe");
                startInfo.WindowStyle = ProcessWindowStyle.Normal;
                Process.Start(startInfo);
            }
            catch
            {
            }
            //startInfo.Arguments = "www.northwindtraders.com";
            //Process.Start(startInfo);
        }


        //CODEEEEE
        private void btnKhachHang_Click(object sender, EventArgs e)
        {
            if (qlKhachHang == null || qlKhachHang.IsDisposed)
            {
                qlKhachHang = new frmKhachHang(QuyenHan);
                qlKhachHang.MdiParent = frmMain_2.ActiveForm;
                qlKhachHang.Show();

            }
            else
                QLCongDan.Activate();
        }

        private void btnNhaCC_Click(object sender, EventArgs e)
        {
            if (qlNhaCungCap == null || qlNhaCungCap.IsDisposed)
            {
                qlNhaCungCap = new frmNhaCC(QuyenHan);
                qlNhaCungCap.MdiParent = frmMain_2.ActiveForm;
                qlNhaCungCap.Show();

            }
            else
                QLCongDan.Activate();
        }

        private void btnSanPham_Click(object sender, EventArgs e)
        {
            if (qlSanPham == null || qlSanPham.IsDisposed)
            {
                qlSanPham = new frmSanPham(QuyenHan);
                qlSanPham.MdiParent = frmMain_2.ActiveForm;
                qlSanPham.Show();

            }
            else
                qlSanPham.Activate();
        }

        private void btnDatHang_Click(object sender, EventArgs e)
        {
            if (qlDatHang == null || qlDatHang.IsDisposed)
            {
                qlDatHang = new frmDatHang(QuyenHan);
                qlDatHang.MdiParent = frmMain_2.ActiveForm;
                qlDatHang.Show();

            }
            else
                qlDatHang.Activate();
        }
        
    }
}
